package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.CustomerMapper;
import com.ruoyi.system.mapper.UserCustomerRelationMapper;
import com.ruoyi.system.domain.Customer;
import com.ruoyi.system.domain.SalesOrder;
import com.ruoyi.system.domain.UserCustomerRelation;
import com.ruoyi.system.service.ICustomerService;

/**
 * 客户管理Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class CustomerServiceImpl implements ICustomerService 
{
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private UserCustomerRelationMapper userCustomerRelationMapper;

    /**
     * 查询客户列表
     * 
     * @param customer 客户信息
     * @return 客户集合
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 查询客户详细信息
     * 
     * @param customerId 客户ID
     * @return 客户信息
     */
    @Override
    public Customer selectCustomerById(Long customerId)
    {
        return customerMapper.selectCustomerById(customerId);
    }

    /**
     * 查询客户关联的销售单
     * 
     * @param customerId 客户ID
     * @return 销售单列表
     */
    @Override
    public List<SalesOrder> selectSalesOrdersByCustomerId(Long customerId)
    {
        return customerMapper.selectSalesOrdersByCustomerId(customerId);
    }

    /**
     * 新增客户
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCustomer(Customer customer)
    {
        int rows = customerMapper.insertCustomer(customer);
        // 新增用户客户关系
        UserCustomerRelation relation = new UserCustomerRelation();
        relation.setUserId(SecurityUtils.getUserId());
        relation.setCustomerId(customer.getCustomerId());
        userCustomerRelationMapper.insertUserCustomerRelation(relation);
        return rows;
    }

    /**
     * 修改客户
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int updateCustomer(Customer customer)
    {
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 删除客户信息
     * 
     * @param customerId 客户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCustomerById(Long customerId)
    {
        // 删除用户客户关系
        userCustomerRelationMapper.deleteUserCustomerRelation(customerId);
        return customerMapper.deleteCustomerById(customerId);
    }

    /**
     * 批量删除客户信息
     * 
     * @param customerIds 需要删除的客户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCustomerByIds(Long[] customerIds)
    {
        for (Long customerId : customerIds)
        {
            // 删除用户客户关系
            userCustomerRelationMapper.deleteUserCustomerRelation(customerId);
        }
        return customerMapper.deleteCustomerByIds(customerIds);
    }
} 